import CONSTANTS from '@/utils/constants';
import moment from 'moment';//引入moment依赖
import md5 from 'js-md5';//引入md5依赖
export function authenticate(url) {
	const requestUrl = url;
	let result = requestUrl.match(/\/([^\/]*)\/(v\d+|\d\.\d)(\/[^\?]+)(\?.*)?/); //将requestUrl分割，形成result数组
	let requestPath = result[0]; //取result数组的第一个元素
	let timeStamp = moment(new Date().getTime()).format("YYYY-MM-DD HH:mm:ss"); //使用moment依赖获取时间戳
	if (requestPath.endsWith("/")) { //判断requestPath是否是以"/"结尾
		requestPath = this.requestPath.substring(0, requestPath.length - 1); //去除最后一位字符
	}
	let tempArray = requestPath.split("?"); //使用"?"作为分隔符分割requestPath
	requestPath = tempArray[0]; //取tempArray数组的第一个元素，组成requestPath
	let sign = CONSTANTS.SECURITY + "callerServiceId" + CONSTANTS.KUMIAPPID + "requestPath" + requestPath + "timestamp" +
		timeStamp + CONSTANTS.SECURITY;
	//拼接sign
	sign = md5(sign); //使用md5依赖进行md5加密
	sign = sign.toUpperCase(); //将小写字母转化为大写
	//以上操作获取了sign和timeStamp
	return {
		"sign": sign,
		"timestamp": timeStamp
	};
}